/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.ComponentModel.Design;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

using Microsoft.VisualStudio.Modeling.Shell;
using Microsoft.Win32;


namespace DslEditorPowerToy.Controls.Languages.ToolWindows
{
    /// <summary>
    /// Provides a base class toolwindow that hosts a user control
    /// Registers and handles selection changes, for both VS and the modeling diagram itself
    /// Acts as a bi-directional selection broker between control and VS.
    /// </summary>
    [CLSCompliant(false)]
    public abstract class ControlHostBase : DslLanguageBase
    {
        ComponentSelectionBroker _selectionBroker = null;

        #region Constructors
        public ControlHostBase(IServiceProvider serviceProvider, Type dslLanguageDocViewType)
            : base(serviceProvider, dslLanguageDocViewType)
        {
        }

        #endregion

        #region Protected Properties
        /// <summary>
        /// Gets the contained control
        /// </summary>
        protected abstract Control Control
        {
            get;
        }
        #endregion
                
        #region Public Properties
        /// <summary>
        /// Gets the window of the control hosted inside the tool window
        /// </summary>
        public override IWin32Window Window
        {
            get
            {
                return this.Control;
            }
        }
        /// <summary>
        /// Gets the title of the tool window
        /// </summary>
        public abstract override string WindowTitle
        {
            get;
        }
        #endregion

        #region Protected Methods
        protected override void Dispose(bool disposing)
        {
            try
            {
                if (disposing)
                {
                    //Unregister from user preference events
                    SystemEvents.UserPreferenceChanged -= this.OnUserPreferenceChanged;

                    if (null != this._selectionBroker)
                    {
                        this._selectionBroker.EnvironmentSelectionChanged -= _selectionBroker_EnvironmentSelectionChanged;
                        this._selectionBroker.Dispose();
                    }
                }
            }
            finally
            {
                base.Dispose(disposing);
            }
        }
        /// <summary>
        /// Initialises this instance of the tool window
        /// </summary>
        protected override void OnToolWindowCreate()
        {
            base.OnToolWindowCreate();

            //Set control appearance properties
            SetControlUserPreferences();

            //Register for preference changes
            SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(this.OnUserPreferenceChanged);

            //Creates a selection broker
            this._selectionBroker = new ComponentSelectionBroker(this, this.Control);
            this._selectionBroker.EnvironmentSelectionChanged += new SelectionChangedEventHandler(_selectionBroker_EnvironmentSelectionChanged);
        }

        /// <summary>
        /// Handles the change in document window
        /// </summary>
        /// <param name="oldView"></param>
        /// <param name="newView"></param>
        protected override void OnDocumentWindowChanged(ModelingDocView oldView, ModelingDocView newView)
        {
            base.OnDocumentWindowChanged(oldView, newView);

            //Register DocView for selection change handling
            if (null != this.DocView)
                this._selectionBroker.RegisterDocView(this.DocView);
        }
        /// <summary>
        /// Handles change in environment selection
        /// </summary>
        protected virtual void OnEnvironmentSelectionChanged(ISelectionProvider selectionProvider, SelectionChangeSource source)
        {
        }
        #endregion

        #region Event Handlers
        /// <summary>
        /// Handles the user preference change event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
        {
            //Set control appearance properties
            SetControlUserPreferences();
        }
        /// <summary>
        /// Handles changes of selection in VS environment
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void _selectionBroker_EnvironmentSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            OnEnvironmentSelectionChanged(e.SelectionProvider, e.Source);
        }
        #endregion

        #region Private Methods
        /// <summary>
        /// Updates the hosted control appearance based upon toolwindow settings
        /// </summary>
        private void SetControlUserPreferences()
        {
            if (null != this.Control)
                this.Control.Font = base.ToolWindowFont;
        }
        #endregion
    }
}
